package com.lv.dhart.ui.fragment.fghome.inner.commend;

import android.view.View;
import android.view.WindowManager;

import com.lv.dhart.R;
import com.lv.dhart.base.AbsTopNavigationTabBaseFragment;
import com.lv.dhart.beans.as4kdots.OrganizationHoldStockCodeBean;
import com.lv.dhart.callback.IOHSLCallback;
import com.lv.dhart.http.rxeasy.utils.HttpLog;
import com.lv.dhart.presenter.ASBaseInfoPresenter;
import com.smart.holder.iinterface.IViewHolderHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by binny on 2018/6/8.
 */

public class StockBaseInfoFragment extends AbsTopNavigationTabBaseFragment<OrganizationHoldStockCodeBean.DataDTO> implements IOHSLCallback {
    protected List<OrganizationHoldStockCodeBean.DataDTO> dataDTOS = new ArrayList<>();
    private final ASBaseInfoPresenter asBaseInfoPresenter = new ASBaseInfoPresenter();
    private int offset = 0;
    private int limit = 10;

    public StockBaseInfoFragment() {
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
    }

    @Override
    protected void afterInitView() {
        super.afterInitView();
        getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        mRefreshLayout.setEnableRefresh(true);
        mRefreshLayout.setEnableLoadMore(true);
    }

    @Override
    protected void getData() {
        asBaseInfoPresenter.getStockBaseInfoList(offset, limit, this);
    }

    @Override
    protected void onRefresh() {
        offset = 0;
        asBaseInfoPresenter.getStockBaseInfoList(offset, limit, this);
    }

    @Override
    protected void onLoadMore() {
        asBaseInfoPresenter.getStockBaseInfoList(offset, limit, this);
    }

    @Override
    protected IViewHolderHelper initViewHolderHelper() {
        return new OHSLViewHolderHelper();
    }

    @Override
    protected int initItemLayout() {
        return R.layout.item_fragment_base_info;
    }

    @Override
    protected List initListBean() {
        return dataDTOS;
    }

    @Override
    public void onOHSLSuccess(OrganizationHoldStockCodeBean organizationHoldStockCodeBean) {
        if (offset == 0) {
            dataDTOS.clear();
        }
        offset = organizationHoldStockCodeBean.getOffset() + limit;
        dataDTOS.addAll(organizationHoldStockCodeBean.getData());
        mCommonAdapter.notifyDataSetChanged();
        mRefreshLayout.finishRefresh(true);
        mRefreshLayout.finishLoadMore(true);
    }

    @Override
    public void onOHSLFailure(Throwable throwable) {
        HttpLog.e(throwable.getMessage());
        mRefreshLayout.finishRefresh(false);
        mRefreshLayout.finishLoadMore(true);
    }

}
